var app = getApp();
// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "支付 ￥ 50元",
    type:'',
    id:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      consult_id:options.consult_id,
    })
    if(options.id!=undefined){
      that.setData({
        id: options.id
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height:res.windowHeight
        })
      }
    })
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'my_coupon',
          act: 'mall_coupon',
          coupon_sort: 1,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval.coupon_list.length>=1) {
            that.setData({
              coupon:true,
              coupon_id:''

            })
          }else{
            app.getLoginSession(function (session) {
              wx.request({
                url: app.api,
                data: {
                  app: 'order',
                  act: 'create_user_order',
                  goods: 'user',
                  consult_id: that.data.consult_id,
                  type: that.data.type,
                  t_session: session
                },
                success: function (res) {
                  var data = res.data;
                  if(data.done&&data.retval){
                    var coupons=app.turn(data.retval.coupons);
                    that.setData({
                      coupon_id:''
                    })
                  }
                }
              })
            })
          }
        }
      })
    })
  },
//选择支付方式
  selectOne:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    if(id==''){
      that.setData({
        type:'',
        title:"支付 ￥ 50元"
      })
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'order',
            act: 'create_user_order',
            goods: 'user',
            consult_id: that.data.consult_id,
            type: that.data.type,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval) {
              that.setData({
                coupon_id: ''
              })
            }
          }
        })
      })
    }else{
      that.setData({
        type: 2,
        title: "咨询券支付"
      })
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'order',
            act: 'create_user_order',
            goods: 'user',
            consult_id: that.data.consult_id,
            type: that.data.type,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval) {
              var coupons = app.turn(data.retval.coupons);
              that.setData({
                coupon_id: coupons[0].coupon_id
              })
            }
          }
        })
      })
    }

   
   
  },
  submit:function(){
    var that = this;
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'order',
          act: 'create_user_order',
          consult_id: that.data.consult_id,
          coupon_id: that.data.coupon_id,
          type: that.data.type,
          t_session: session
        },

        // 请求头
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },

        // 请求方法（后端根据method来判断是提交还是确认）
        method: 'POST',
        success: function (res) {
          var data = res.data;
          if(data.done&&data.retval){
            if(data.retval.coupon_id!="")
            {
              wx.redirectTo({
                url: '/pages/docpaysuccess/docpaysuccess?coupon_id=' + data.retval.coupon_id + "&&consult_id=" + data.retval.consult_id+"&&id="+that.data.id,
              })
              
            }else{
              that.callToPay(that.data.consult_id, 50);
             
            }
          }
        }
      })
    })
  },
  // 获取支付信息
  callToPay: function (orderId, amount, fn) {

    var that = this;

    if (isNaN(orderId)) {
      return
    }

    that.getLoginSession(function (session) {
      // 请求
      wx.request({

        // 请求地址
        url: that.api,

        // 请求参数
        data: {
          app: 'cashier',
          act: 'index',
          order_id: orderId,
          t_session: session
        },

        // 请求成功
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            var result = data.retval;
            console.log(result);

            // 优惠券抵消为0时直接到成功页
            if (amount == 0 && result.isPay && result.isPay == 1) {
              wx.navigateTo({
                url: '/pages/paySuccess/paySuccess',
              })
              return
            }
            // 唤起支付功能
            wx.requestPayment({
              'timeStamp': result.timeStamp,
              'nonceStr': result.nonceStr,
              'package': result.package,
              'signType': 'MD5',
              'paySign': result.paySign,
              'success': function (res) {
                console.log(res);
                wx.navigateTo({
                  url: '/pages/paySuccess/paySuccess',
                })
              },
              'fail': function (res) {
                console.log(res);
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '支付失败'
                })
              }
            });
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '获取支付信息失败'
            })
          }
        }
      })

    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})